package service

import (
	"crypto/md5"
	"engine/dto"
	"fmt"
	"golang.org/x/exp/rand"
	"io"
	"time"
)

func GetUserQ(q string) string {
	if q == "" {
		q = RandomString(6)
	}
	count := dto.GetSysUserCountById(q)
	if count > 0 {
		q = RandomString(6)
		return GetUserQ(q)
	}
	return q
}

func RandomString(n int) string {
	randStr := time.Now().UnixNano()
	var letters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
	rand.Seed(uint64(randStr))
	str := make([]byte, n)
	for i := range str {
		str[i] = letters[rand.Intn(len(letters))]
	}
	return string(str)
}

func RandomInt(n int) string {
	randStr := time.Now().UnixNano()
	var letters = []byte("0123456789")
	rand.Seed(uint64(randStr))
	str := make([]byte, n)
	for i := range str {
		str[i] = letters[rand.Intn(len(letters))]
	}
	return string(str)
}
func Md5PayPwd(pwd string) (string, error) {

	w := md5.New()         // 初始化一个MD5对象
	io.WriteString(w, pwd) // 将str写入到w中
	md5str2 := fmt.Sprintf("%x", w.Sum(nil))

	newPwdHK := md5.New()
	io.WriteString(newPwdHK, md5str2)
	newPwdStr := fmt.Sprintf("%x", newPwdHK.Sum(nil))

	return newPwdStr, nil
}

func ToMd5(pwd string) (string, error) {

	w := md5.New()         // 初始化一个MD5对象
	io.WriteString(w, pwd) // 将str写入到w中
	md5str2 := fmt.Sprintf("%x", w.Sum(nil))

	return md5str2, nil
}
